package com.haochang.chunkvod.controller.activity.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.order.PhotoDetailAdapter;
import com.haochang.chunkvod.model.PhotoDetailInfo;
import com.haochang.chunkvod.model.order.OrderData;
import com.haochang.chunkvod.view.thirdparty.photoview.PhotoView;
import com.haochang.chunkvod.view.vod.pager.HackyViewPager;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

/**
 * @author qintao
 *         created at 2016/11/18 17:12
 *         图片查看器 用的uk.co.senab.photoview开源代码
 */

public class PhotoLookOverActivity extends BaseActivity
{
	private TitleView titleView;
	private List<PhotoDetailInfo> dataSource;
	private HackyViewPager viewPager;
	private PhotoDetailAdapter adapter;
	private int position = 0;
	private OrderData orderData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		orderData = new OrderData(PhotoLookOverActivity.this);
		dataSource = new ArrayList<PhotoDetailInfo>();
		//		for (int i = 1; i < 12; i++)
		//		{
		//			PhotoDetailInfo detailInfo = new PhotoDetailInfo();
		//			detailInfo.setUrl("assets://mexport" + i + ".jpg");
		//			dataSource.add(detailInfo);
		//		}
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_photo_look_over);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
			}
		});
		viewPager = (HackyViewPager) findViewById(R.id.viewPager);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		adapter = new PhotoDetailAdapter(dataSource, this);
		viewPager.setAdapter(adapter);
		viewPager.setCurrentItem(position);
		titleView.setTitle("图片");
	}

	@Override
	public void onResume()
	{
		super.onResume();
		requestPictureList();
	}

	/**
	 * 请求网络图片
	 */
	private void requestPictureList()
	{
		orderData.requestPictureList(new OrderData.ResultListListener()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				dataSource.clear();
				dataSource.addAll(list);
				adapter.notifyDataSetChanged();
				if (!CollectionUtils.isEmpty(dataSource) && position < dataSource.size())
				{
					String text = getString(R.string.photo_detail_indicator, position + 1, dataSource.size());
					titleView.setTitle(text);
				} else
				{
					ToastUtils.showText(PhotoLookOverActivity.this, "暂无图片");
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				if (CollectionUtils.isEmpty(dataSource) && !TextUtils.isEmpty(errMsg))
				{
					ToastUtils.showText(PhotoLookOverActivity.this, errMsg);
				}
			}
		});
	}

	class MyOnPageChangeListener implements ViewPager.OnPageChangeListener
	{

		@Override
		public void onPageScrollStateChanged(int arg0)
		{
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2)
		{
		}

		@Override
		public void onPageSelected(int position)
		{
			View view = viewPager.findViewWithTag(position);
			View view_pre = viewPager.findViewWithTag(position - 1);
			View view_next = viewPager.findViewWithTag(position + 1);
			if (view != null && view instanceof PhotoView)
				((PhotoView) view).setScale(1.0f);
			if (view_pre != null && view_pre instanceof PhotoView)
				((PhotoView) view_pre).setScale(1.0f);
			if (view_next != null && view_next instanceof PhotoView)
				((PhotoView) view_next).setScale(1.0f);
			if (!CollectionUtils.isEmpty(dataSource) && position < dataSource.size())
			{
				String text = getString(R.string.photo_detail_indicator, position + 1, dataSource.size());
				titleView.setTitle(text);
			}
		}
	}

}
